package techdemo;

import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;

import addon.w3.mdx.MDXMasterController;
import addon.w3.mdx.MDXToJme;
import addon.w3.mdx.TestMdxToJmeWrite;

import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.scene.state.ZBufferState;
import com.jme.system.DisplaySystem;
import com.jmex.audio.AudioSystem;
import com.jmex.audio.AudioTrack;
import com.jmex.audio.AudioTrack.TrackType;
import com.jmex.audio.MusicTrackQueue.RepeatType;
import com.jmex.effects.transients.Fader;
import com.jmex.font2d.Font2D;

import chaos.main.CBFSystem;
import chaos.main.GCore;
import chaos.util.AbsPathResourceManager;
import chaos.util.MPQResourceManager;
import chaos.util.SmartResourceManager;
import chaos.util.ZIPResourceManager;

public class CBFFirstScreen implements GCore {
	private Fader fader;
	CBFSystem gameState = null;
	Node myNode = null;
	AudioSystem audio = null;
	boolean sourceLightState = false;
	
	public String getName() {
		return "CBF:FirstScreen";
	}

	public void updateCore(float time) {
		// TODO Auto-generated method stub
		// update our audio system here:
        audio.update();
        
        if(KeyBindingManager.getKeyBindingManager().isValidCommand("startTechDemo",false)) {
        	gameState.deactiveGCore("CBF:StartMenu");
        	fader = new Fader("Fader", DisplaySystem.getDisplaySystem().getWidth(), DisplaySystem.getDisplaySystem().getHeight(), new ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f), 2.0f);
    		fader.setAlpha(0.0f);
    		fader.setZOrder(0);
    		fader.setMode(Fader.FadeMode.FadeOut);
    		ZBufferState zb = gameState.getDisplay().getRenderer().createZBufferState();
    		zb.setEnabled(true);
    		zb.setWritable(true);
    		zb.setFunction(ZBufferState.TestFunction.Always);
    		fader.setRenderState(zb);
    		fader.updateRenderState();
    		gameState.getRootNode().attachChild(fader);
        }
        
        if ( fader != null && fader.getAlpha() == 1.0f ) {
			// If the fader gets to 1.0f we'll switch to fade back in
        	gameState.deactiveGCore(this);
		}
	}
    
    public void init(CBFSystem gameState) {
        try {
        	this.gameState = gameState;
        	
        	myNode = new Node(getName());
        	MDXToJme converter = new MDXToJme(MPQResourceManager.singleton());
        	converter.load("Buildings\\Other\\DarkPortal\\DarkPortal.mdx");
            Spatial r = converter.constructMesh();
                        
        	MDXMasterController controller = (MDXMasterController)converter.getController();
            controller.setSequence("Stand Work");
            controller.setRepeat(-1);
            r.addController( controller );
            myNode.attachChild(r);
            gameState.getCam().setFrustumPerspective(45.0f, (float) gameState.getDisplay().getWidth()
                    / (float) gameState.getDisplay().getHeight(), 1f, 10000f);//farPlane);//
            gameState.getCam().setLocation(new Vector3f(.0f, 0.0f, 250));
            
            // grab a handle to our audio system.
            audio = AudioSystem.getSystem();
            //set background sound
            // setup a music score for our demo
            AudioTrack sound = AudioSystem.getSystem().createAudioTrack(new java.io.File("F:\\Music\\gm\\jericho\\04 - Path of the Righteous.ogg").toURL(), true);
            sound.setType(TrackType.MUSIC);
            sound.setRelative(true);
            sound.setTargetVolume(0.7f);
            sound.setLooping(false);
            audio.getMusicQueue().setRepeatType(RepeatType.ALL);
            audio.getMusicQueue().setCrossfadeinTime(2.5f);
            audio.getMusicQueue().setCrossfadeoutTime(2.5f);
            audio.getMusicQueue().addTrack(sound);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void beforeActivate() {
    	gameState.getInput().removeAllFromAttachedHandlers();
    	gameState.getDisplay().getRenderer().setBackgroundColor( ColorRGBA.black );
    	gameState.getRootNode().attachChild(myNode);
    	myNode.getLocalRotation().fromAngles(-1.5f, -1.2f, -0.1f);
    	
    	gameState.getCam().setFrustumPerspective(45.0f, (float) gameState.getDisplay().getWidth()/ (float) gameState.getDisplay().getHeight(), 1f, 10000f);//farPlane);//
    	
    	gameState.getCam().setLocation(new Vector3f(-160.0f, 200.0f, 650));
    	gameState.getCam().lookAt(new Vector3f(0, 200, 0), Vector3f.UNIT_Y);
    	
    	gameState.getCam().update();
    	audio.getMusicQueue().play();
    	
    	KeyBindingManager.getKeyBindingManager().set("startTechDemo", KeyInput.KEY_K);
    	
    	this.sourceLightState = gameState.getLightState().isEnabled();
    	gameState.getLightState().setEnabled(true);
    }
    
    public void beforeDeactive() {
    	gameState.getRootNode().detachChild(fader);
    	fader = null;
    	KeyBindingManager.getKeyBindingManager().remove("startTechDemo");
    	audio.getMusicQueue().stop();
    	gameState.getLightState().setEnabled(sourceLightState);
    	gameState.getRootNode().detachChild(myNode);
    	
    	
    	gameState.activeGCore("CBF:InGame");
    }
}